import { LoadState } from './load.models';
import { Action, createReducer, on } from '@ngrx/store';
import { actionLoadFinish, actionLoadStart } from '@core/interceptors/load.actions';

// 初始加载状态
export const initialState: LoadState = {
  isLoading: false
};

// 创建 reducer，处理加载状态的变化
const reducer = createReducer(
  initialState,
  // 开始加载时设置为 true
  on(actionLoadStart, state => ({ ...state, isLoading: true })),
  // 结束加载时设置为 false
  on(actionLoadFinish, state => ({ ...state, isLoading: false }))
);

// 导出 reducer 函数
export function loadReducer(state: LoadState | undefined, action: Action): LoadState {
  return reducer(state, action);
}
